/*
    fracgen-c : Fractal Generator (using concurrency)
    Copyright (C) 2010 Arpit Sud, Sri Teja Basava & Sidartha Gracias

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package fractal;

import util.Complex;
import complexPlane.Axes;

public class Mandelbrot extends EscapeTimeFractal 
{
	private static final Axes AXES = new Axes(-2.0, 2.0, -2.0, 2.0);
	private static final double LIMIT = 2.0;
	private static final int DEFAULT_ITERATIONS = 100;
	
	public Mandelbrot()
	{
		super(AXES, LIMIT, DEFAULT_ITERATIONS);
	}
	
	public Mandelbrot(int numIterations, int[] colors)
	{
		super(AXES, LIMIT, numIterations, colors);
	}
	
	public String name()
	{
		return "Mandelbrot";
	}
	
	public String[] parameters()
	{
		return new String[0];
	}
	
	public int getBW(Complex c)
	{
		Complex z;
		int i;
		
		if (c == null)
			throw new IllegalArgumentException();
		
		z = new Complex(0.0, 0.0);
		for (i = 0; i < _numIterations; ++i)
		{
			z = (z.multiply(z)).add(c);
			if (z.magnitude() > _limit) /* if fractal equation has diverged */
				return 0xFFFFFF; // White
		}
		
		/* if fractal equation has converged */
		return 0x000000; // Black
	}

	public int getColor(Complex c)
	{
		Complex z;
		int i;
		
		if (c == null)
			throw new IllegalArgumentException();
		
		z = new Complex(0.0, 0.0);
		for (i = 0; i < _numIterations; ++i)
		{
			z = (z.multiply(z)).add(c);
			if (z.magnitude() > _limit) /* if fractal equation has diverged */
				return _colors[(i * _colors.length) / _numIterations];
		}
		
		/* if fractal equation has converged */
		return 0x000000; // Black
	}
}
